package com.utils;

import java.io.IOException;
import java.util.Map;

import com.alibaba.fastjson.JSON;

import sun.misc.BASE64Decoder;

public class Base64Utils {
    
    public static String base64Decode(String s) throws IOException {
        BASE64Decoder decoder = new BASE64Decoder();
        byte[] bs = decoder.decodeBuffer(s);
        for (int i = 0; i < bs.length; i++) {
            if (bs[i] < 0) {
                int tmp27_26 = i;
                byte[] tmp27_25 = bs;
                tmp27_25[tmp27_26] = ((byte) (tmp27_25[tmp27_26] + 256));
            }
        }
        return new String(bs, "UTF-8");
    }
    
    public static String decode(String s) {
        byte[] b = null;
        String result = null;
        if (s != null) {
            BASE64Decoder decoder = new BASE64Decoder();
            try {
                b = decoder.decodeBuffer(s);
                result = new String(b, "utf-8");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return result;
    }
    
    @SuppressWarnings("unchecked")
    public static Map<String, String> decode2(String s) {
        String result = decode(decode(s));
        //Lg.log("base decode2",result);
        //string 转 map 集合
        return JSON.parseObject(result, Map.class);
    }
    
    @SuppressWarnings("unchecked")
    public static String decode5(String s) {
        String result = decode(decode(s));
        //Lg.log("base decode2",result);
        //string 转 map 集合
        return result;
    }
    
    /**
     * 用于filter解密
     *
     * @param s
     * @return
     */
    @SuppressWarnings("unchecked")
    public static Map<String, String[]> decode3(String s) {
        String result = decode(decode(s));
        //string 转 map 集合
        return JSON.parseObject(result, Map.class);
    }
    
    public static void main(String[] args) {
        String s = "WVdGaFBURW1ZbUppUFRJbVkyTmpQVE09";
        String ii = decode(decode(s));
        
        System.out.println(ii);
        //System.out.println(stringStringMap);
        Map<String, String> map = JSON.parseObject("{code:\"1\",datas:[{code:\"1\"},{code:\"2\"}]}", Map.class);
        for (Map.Entry<String, String> entry : map.entrySet()) {
            System.out.println(entry.getKey() + "  " + entry.getValue());
        }
    }
}
